Imports System.Drawing.Drawing2D

Friend Class gaugeRPM
    Private m_xoffset As Integer
    Private m_yoffset As Integer = -5

    Private mGraphicManager As BufferedGraphicsContext = Nothing
    Private mBackgroundBuffer As BufferedGraphics = Nothing

    Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        DrawPoints(Needle1)
    End Sub

    Public Property Value() As Double
        Get
            Return Needle1.Value
        End Get
        Set(ByVal value As Double)
            If value <> Needle1.Value Then
                Needle1.Value = value
                Me.Refresh()
            End If
        End Set
    End Property
    Public Property subSize() As Double
        Get
            Return Needle1.Length
        End Get
        Set(ByVal value As Double)
            Needle1.Length = value
            Me.Refresh()
        End Set
    End Property
    Public Property offsetX() As Integer
        Get
            Return m_xoffset
        End Get
        Set(ByVal value As Integer)
            m_xoffset = value
            Call Me.gaugeRPM_Resize(Nothing, Nothing)
            Me.Refresh()
        End Set
    End Property
    Public Property offsetY() As Integer
        Get
            Return m_yoffset
        End Get
        Set(ByVal value As Integer)
            m_yoffset = value
            Call Me.gaugeRPM_Resize(Nothing, Nothing)
            Me.Refresh()
        End Set
    End Property

    Private Sub gaugeRPM_Resize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Resize
        Needle1.Bounds = New Rectangle(m_xoffset, m_yoffset, Me.Width, Me.Height)
        DrawPoints(Needle1)
        Me.Refresh()
    End Sub
    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        If Not IsNothing(mBackgroundBuffer) Then mBackgroundBuffer.Render(e.Graphics)
        MyBase.OnPaint(e)
    End Sub
    Private Sub DrawPoints(ByRef sender As ShapeLibrary.Needle) Handles Needle1.onRedrewPoints
        mGraphicManager = BufferedGraphicsManager.Current
        mGraphicManager.MaximumBuffer = New Size(Me.Width + 1, Me.Height + 1)
        mBackgroundBuffer = mGraphicManager.Allocate(Me.CreateGraphics, Me.ClientRectangle)
        mBackgroundBuffer.Graphics.Clear(Me.BackColor)
        If Not IsNothing(Me.BackgroundImage) Then mBackgroundBuffer.Graphics.DrawImage(Me.BackgroundImage, New PointF(0, 0))
        sender.drawGaugePoints(mBackgroundBuffer.Graphics)
    End Sub

End Class